angular.module('wf-utils')
.service('menuManage', ($rootScope,$http)->
    states = {
        #欢迎页/控制台
        'welcome':{
            name:'欢迎页面',
            icon:'fa-home',
            url:'welcome/welcome.html',
            controller:'WFWelcomeCtrl'
        },
        #系统
        'role-manage': {
            name: '角色配置',
            url: "system/role/role-manage.html",
            controller: "WFRoleManageCtrl"
        },
        'role-setting':{
            name: '角色菜单配置',
            url: "system/role/role-setting.html",
            controller: "WFRoleSettingCtrl"
        },
        'menu-manage': {
            name: '菜单配置',
            url: "system/menu/menu-manage.html",
            controller: "WFMenuManageCtrl"
        },
        'account-manage': {
            name: '账号配置',
            url: "system/account/account-manage.html",
            controller: "WFAccountManageCtrl"
        },
        'account-manage-edit': {
            name: '账号明细编辑',
            url: "system/account/account-manage-edit.html",
            controller: "WFAccountManageEditCtrl"
        },
        'authority-manage': {
            name: '权限配置',
            url: "system/authority/authority-manage.html",
            controller: "WFAuthorityManageCtrl"
        },
        'authority-manage-edit': {
            name: '权限编辑页面',
            url: "system/authority/authority-manage-edit.html",
            controller: "WFAuthorityManageEditCtrl"
        },
        'resource-manage': {
            name: '资源配置',
            url: "system/resource/resource-manage.html",
            controller: "WFResourceManageCtrl"
        },
        #参数配置
        'basic-params':{
            name: '参数配置',
            url: "system/parameters/basic-params.html",
            controller: "WFBasicParamsCtrl"
        }
        'params-setting':{
            name: '参数字典配置',
            url: "system/parameters/params-setting.html",
            controller: "WFParamsSettingCtrl"
        }
        #青果寓业务管理
        'apartment-manage':{
            name: '公寓管理',
            url: "project-manage/apartment-manage/apartment-manage.html",
            controller: "QgyApartmentManageCtrl"
        }
        'floor-plans-manage':{
            name: '楼层维护',
            url: "project-manage/floor-plans-manage/floor-plans-manage.html",
            controller: "QgyFloorPlansManageCtrl"
        }
        'apartment-dynamic-config':{
            name: '楼栋配置',
            url: "project-manage/apartment-dynamic-config/apartment-dynamic-config.html",
            controller: "QgyDynamicConfigCtrl"
        }
        'apartment-layout-manage':{
            name: '房型管理',
            url: "project-manage/apartment-layout-manage/layout-manage.html",
            controller: "QgyApartmentLayoutManageCtrl"
        }
        'apartment-configure-type':{
            name: '房间标配',
            url: "project-manage/apartment-configure-type/apartment-configure-type.html",
            controller: "QgyConfigureTypeCtrl"
        }
        'layout-picture':{
            name: '房型图片管理',
            url: "project-manage/apartment-layout-manage/layout-picture.html",
            controller: "QgyLayoutPictureCtrl"
        }
        'apartment-picture':{
            name: '公寓图片管理',
            url: "project-manage/apartment-manage/apartment-picture.html",
            controller: "QgyApartmentPictureCtrl"
        }
        'appointment-manage':{
            name: '预约管理',
            url: "business-manage/appointment-manage/appointment-manage.html",
            controller: "QgyAppointManageCtrl"
        }
        'building-manage':{
            name: '楼栋管理',
            url: "project-manage/apartment-building-manage/building-manage.html",
            controller: "QgyApartmentBuildingManageCtrl"
        }
        'room-manage':{
            name: '房间管理',
            url: "project-manage/apartment-room-manage/room-manage.html",
            controller: "QgyApartmentRoomManageCtrl"
        }
        'contract-manage':{
            name: '合同管理',
            url: "business-manage/contract-manage/contract-manage.html",
            controller: "QgyContractManageCtrl"
        }
        'contract-detail':{
            name: '合同详情',
            url: "business-manage/contract-manage/contract-detail.html",
            controller: "QgyContractDetailCtrl"
        }
        'contract-edit':{
            name: '合同编辑',
            url: "business-manage/contract-edit/contract-edit.html",
            controller: "QgyContractEditCtrl"
        }
        'check-in':{
            name: '快捷入住',
            url: "business-manage/check-in/check-in.html",
            controller: "QgyCheckInCtrl"
        }
        'rent-bill-manage':{
            name: '房租账单',
            url: "business-manage/rent-bill-manage/bill-manage.html",
            controller: "QgyRentBillManageCtrl"
        }
        'online-check-in':{
            name: '线上入住',
            url: "business-manage/online-check/online-check.html",
            controller: "QgyOnlineCheckCtrl"
        }
        'offline-check-in':{
            name: '线下入住',
            url: "business-manage/offline-check/offline-check.html",
            controller: "QgyOfflineCheckCtrl"
        }
        'house-manage':{
            name: '房态管理',
            url: "house-manage/house-manage.html",
            controller: "QgyHouseManageCtrl"
        }
        'off-lease-manage':{
            name: '退租管理',
            url: "business-manage/off-lease-manage/off-lease-manage.html",
            controller: "QgyOffLeaseManageCtrl"
        }
        'off-lease-confirm':{
            name: '退租确认',
            url: "house-manage/off-lease-confirm/off-lease-confirm.html",
            controller: "QgyOffLeaseConfirmCtrl"
        }
        'house-detail':{
            name: '房间明细',
            url: "house-manage/house-detail/house-detail.html",
            controller: "QgyHouseDetailCtrl"
        }
        'house-message':{
            name: '房间动态',
            url: "business-manage/house-message/house-message.html",
            controller: "QgyHouseMessageCtrl"
        }
        'cotenant-manage':{
            name: '住客管理',
            url: "business-manage/cotenant-manage/cotenant-manage.html",
            controller: "QgyCotenantManageCtrl"
        }
        'finance-audit':{
            name: '财务审核',
            url: "finance-service/finance-audit/finance-audit.html",
            controller: "QgyFinanceAuditCtrl"
        }
        'finance-statistic':{
            name: '财务报表',
            url: "finance-service/finance-statistic/finance-statistic.html",
            controller: "QgyFinanceStatisticCtrl"
        }
        'commission-manage':{
            name: '佣金管理',
            url: "finance-service/commission-manage/commission-manage.html",
            controller: "QgyCommissionManageCtrl"
        }
        'earnest-manage':{
            name: '定金管理',
            url: "business-manage/earnest-bill-manage/bill-manage.html",
            controller: "QgyEarnestBillManageCtrl"
        }
        'relet-check-in':{
            name: '到期续租',
            url: "house-manage/relet-check-in/relet-check-in.html",
            controller: "QgyReLetCheckInCtrl"
        }
        'change-house-check-in':{
            name: '换房',
            url: "house-manage/change-house-check-in/change-house-check-in.html",
            controller: "QgyChangeHouseCheckInCtrl"
        }
        'earnest-check-in':{
            name: '定金入住',
            url: "house-manage/earnest-check-in/earnest-check.html",
            controller: "QgyEarnestCheckInCtrl"
        }
        'sublet-check-in':{
            name: '转租',
            url: "house-manage/sublet-check-in/sublet-check-in.html",
            controller: "QgySubLetCheckInCtrl"
        }
        'repair-manage':{
            name: '报修管理',
            url: "business-manage/repair-manage/repair-manage.html",
            controller: "QgyRepairManageCtrl"
        }
        'suggestion-manage':{
            name: '投诉处理',
            url: "business-manage/suggestion-manage/suggestion-manage.html",
            controller: "QgySuggestionManageCtrl"
        }
        'member-manage':{
            name: '会员管理',
            url: "business-manage/member-manage/member-manage.html",
            controller: "QgyMemberManageCtrl"
        }
        'member-detail':{
            name: '会员明细',
            url: "business-manage/member-detail/member-detail.html",
            controller: "QgyMemberDetailCtrl"
        }
        'rental-good':{
            name: '租赁产品',
            url: "good-manage/rental-good/rental-good.html",
            controller: "QgyRentalGoodCtrl"
        }
        'cleaning-good':{
            name: '保洁产品',
            url: "good-manage/cleaning-good/cleaning-good.html",
            controller: "QgyCleaningGoodCtrl"
        }
        'score-good':{
            name: '积分产品',
            url: "good-manage/score-good/score-good.html",
            controller: "QgyScoreGoodCtrl"
        }
        'cleaning-order':{
            name: '保洁订单',
            url: "good-manage/cleaning-order/cleaning-order.html",
            controller: "QgyCleaningOrderCtrl"
        }
        'rental-order':{
            name: '租赁订单',
            url: "good-manage/rental-order/rental-order.html",
            controller: "QgyRentalOrderCtrl"
        }
        'score-order':{
            name: '积分订单',
            url: "good-manage/score-order/score-order.html",
            controller: "QgyScoreOrderCtrl"
        }
        #活动
        'active-create':{
            name: '活动创建',
            url: "active/active-create/active-create.html",
            controller: "QgyActiveCreateCtrl"
        }
        'active-manage':{
            name: '活动管理',
            url: "active/active-manage/active-manage.html",
            controller: "QgyActiveManageCtrl"
        }
        'active-detail':{
            name: '活动明细',
            url: "active/active-detail/active-detail.html",
            controller: "QgyActiveDetailCtrl"
        }

        ##价格体系
        'promotion-manage':{
            name: '优惠活动',
            url: "price-manage/promotion-manage/promotion-manage.html",
            controller: "QgyPromotionManageCtrl"
        }
        'price-strategy':{
            name: '定价管理',
            url: "price-manage/price-strategy/price-strategy.html",
            controller: "QgyPriceStrategyCtrl"
        }
        'rebate-manage':{
            name: '返利规则',
            url: "price-manage/rebate-manage/rebate-manage.html",
            controller: "QgyRebateManageCtrl"
        }
        #充值记录
        'recharge':{
            name: '充值记录',
            url: "finance-service/recharge/recharge-manage.html",
            controller: "EdRechargeManageCtrl"
        }
        'notification-manage':{
            name: '通知管理',
            url: "business-manage/notification-manage/notification-manage.html",
            controller: "QgyNotificationManageCtrl"
        }
        'corporate-infomation':{
            name: '企业资讯',
            url: "business-manage/corporate-infomation/corporate-infomation.html",
            controller: "QgyCorporateInformationCtrl"
        }
        'my-message':{
            name: '我的消息',
            url: "business-manage/my-message/my-message.html",
            controller: "QgyMyMessageCtrl"
        }



    }
    menuList = [
        {
            id: 1,
            href: 'welcome',
            name: '首页',
            subMenus: []
        },
        {
            id: 2,
            href: '#',
            isLeaf:false,
            name: '系统管理',
            subMenus: [
                {id:21,href: 'account-manage', name: '账号管理'},
                {id:22,href: 'role-manage', name: '角色管理'},
                {id:23,href: 'menu-manage', name: '菜单管理'},
                {id:24,href: 'authority-manage', name: '权限管理'},
                {id:25,href: 'resource-manage', name: '资源管理'}
            ]
        }
    ]
    get: ()->

        menuList

    getRoleMenu: (roleID)->
        menuList
    goState:(state,reload)->
        if states[state]
            item={
                href:state
                name:states[state].name
                icon:states[state].icon
            }
            this.goPage(item,reload)
    goPage: (item,reload)->
        href = item.href
        if $rootScope.navTabs.active == href
            return
        tmp = _.find $rootScope.navTabs.arrs, (tab)-> tab.href == href
        if !tmp
            $rootScope.navTabs.arrs.push {href: href, name: item.name, icon: item.icon}
        else
            if reload
                $rootScope.navTabs.arrs=_.filter($rootScope.navTabs.arrs,(tab)->tab.href!=href)
                $rootScope.navTabs.history = _.without $rootScope.navTabs.history, href
                $rootScope.navTabs.arrs.push {href: href, name: item.name, icon: item.icon}
        $rootScope.navTabs.active = href
        tmpHistory = _.find $rootScope.navTabs.history, (history)-> history == href
        if tmpHistory
            $rootScope.navTabs.history = _.without $rootScope.navTabs.history, tmpHistory
        $rootScope.navTabs.history.push href
#        console.log('navTabs:', $rootScope.navTabs)
    genViews: ()->
        views = {"":{templateUrl:'templates/main/main.html',controller:'WFMainCtrl'}}
        _.each states, (item,param)->
#            console.log('state:',param)
            views[param + '@main'] = {
                templateUrl: 'templates/' + item.url
                controller: item.controller
            }
#        console.log(views)
        views
    closeTab:(event,href)->
        if event
            event.preventDefault()
        $rootScope.navTabs.history=_.filter $rootScope.navTabs.history,(history)->history!=href
        $rootScope.navTabs.arrs=_.filter $rootScope.navTabs.arrs,(tab)->tab.href!=href
        if $rootScope.navTabs.active==href and $rootScope.navTabs.history.length>=1
            $rootScope.navTabs.active=$rootScope.navTabs.history[$rootScope.navTabs.history.length-1]
)